package com.tequila.bops.controller;

import com.tequila.core.service.StoreManagerService;
import com.tequila.domain.dto.ManagerDto;
import com.tequila.domain.dto.StoreManagerDto;
import com.tequila.domain.entity.StoreManager;
import com.tequila.domain.enums.StateEnum;
import com.tequila.domain.query.StoreManagerQuery;
import fe.core.ApiResponse;
import fe.core.Assert;
import fe.core.exception.BizException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/storeManager")
public class StoreManagerController extends BasicController {

    @Resource
    private StoreManagerService storeManagerService;

    @RequestMapping("/doLogin")
    public ApiResponse doLogin(String account,String password,@RequestParam(required = false) boolean remember) {
        Assert.assertNotNull(account,"账号不能为空!");
        Assert.assertNotNull(password,"密码不能为空!");
        StoreManagerDto storeManagerDto = storeManagerService.queryStoreManagerByPhone(account);
        Assert.assertNotNull(storeManagerDto,"账号不存在!");
        Assert.assertNotEquals(storeManagerDto.getPassword(),password,"密码错误!");

        if (storeManagerDto.getState().equals(StateEnum.DISABLE.getCode())) throw new BizException("账号已被禁用!");
        return ApiResponse.successApiResponse().withData(createUserDetail(storeManagerDto.getId(),storeManagerDto.getPhone(),storeManagerDto.getPassword(),remember,storeManagerDto));
    }

    @RequestMapping("/queryPageListByParam")
    public ApiResponse queryPageListByParam(StoreManagerQuery query) {
        return ApiResponse.successApiResponse().withData(storeManagerService.queryPageListByParam(query));
    }

    @RequestMapping("/queryById")
    public ApiResponse queryById(Long id) {
        return ApiResponse.successApiResponse().withData(storeManagerService.queryById(id));
    }

    @RequestMapping("/save")
    public ApiResponse save(StoreManager entity) {
        return ApiResponse.successApiResponse().withData(storeManagerService.save(entity));
    }

    @RequestMapping("/update")
    public ApiResponse update(StoreManager entity) {
        return ApiResponse.successApiResponse().withData(storeManagerService.updateSelectiveById(entity));
    }
}
